<template>
    <div class="tree Tasktree">
<!--      <el-tree
        :data="TreeData"
        show-checkbox
        node-key="id"
        ref="tree"
        :props="defaultProps"
        @check="handleCheckChange">
        </el-tree> -->
				<el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
						<el-checkbox v-for="(item,index) in this.taskTreeData" :label="item.item_id" :key="index">{{item.item_name}}</el-checkbox>
				</el-checkbox-group>
    </div>
</template>
<script>
import SubDataSetService from '../../../services/SubDataSetService.js'
import gcoord from 'gcoord'
import bus from '../bus';
export default {
    props: ['infoData'],
    data() {
      return {
        SubDataSetService:new SubDataSetService(), 
        infoDataInfo:this.infoData,
        TreeData:[],
        TreeDataInfo:[],
        defaultProps: {
          children: 'children',
          label: 'item_name'
        },
        getCheckedNodes:[],
        GisData:[],
        NewDataList:[],
        NewData:[],
     
        commonData:[],
        treeMapData:[],
        tableMapData:[],
        MapPoint:[],
        SelectedState:'',
        TemporaryPoint:'',
				
				//重构
				batchData:{},
				checkedCities:[],
				cities:[],
				taskTreeData:[],
				taskCheckedNodes:[],
				infoDatas:'',
      };
    },
    watch:{
    },
    methods: {
      //获取树数据
      async DataSet(){
        let user = localStorage.getItem('zhdq_gismanage_dataset');
        let parmes={
          data_set_id:JSON.parse(user).data_set_id
        }
        var results = await this.SubDataSetService.getAll(parmes);
        let result=this.$getWords(104);
        for(let i=0;i<results.data.length;i++){
          let resultInfo= result.map((item)=>{
            return {id:item.item_id+results.data[i].sub_set_id,item_id: item.item_id, item_name: item.item_name}
          })
          let treeList={
              id:results.data[i].sub_set_id,
              item_name:results.data[i].sub_set_name,
              children:resultInfo  
            }
            this.TreeDataInfo.push(treeList)
            if(this.infoData!=null&&this.TreeDataInfo[i].id==this.infoData.sub_set_id){
              let countData=this.$route.path=='/taskModule'?this.infoData.extend_info.task_info.count:this.infoData.extend_info.audit_info.count;
              let childrenData=this.TreeDataInfo[i].children;
              for(let j=0;j<childrenData.length;j++){
                for (var k in countData) {
                  if(k==childrenData[j].item_id){
                     this.TreeDataInfo[i].children[j].item_name=childrenData[j].item_name+'('+countData[k]+')'
                  }
                }
              }
            }
            this.TreeData=this.TreeDataInfo;
          let GisDataList={
              sub_set_id:results.data[i].sub_set_id,
              geo_layers:[]
          }
            this.GisData.push(GisDataList)
        }
      },
       handleCheckChange(checkedNodes,checkedKeys) {
        // 获取父级ID
        let checkedState=this.$refs.tree.getNode(checkedNodes.id).checked;
        let fatherID=this.$refs.tree.getNode(checkedNodes.id).parent.data.id;
        for(let i=0;i<this.GisData.length;i++){
          if(fatherID!=undefined){
              this.NewDataList.push(checkedNodes.item_id);
              this.NewDataList= this.unique(this.NewDataList);
              if(fatherID==this.GisData[i].sub_set_id){ 
                this.GisData[i].geo_layers.push(checkedNodes.item_id);
                this.GisData[i].geo_layers = this.unique(this.GisData[i].geo_layers);
              }
          }else{
            if(checkedNodes.id==this.GisData[i].sub_set_id){
              if(this.$refs.tree.getNode(checkedNodes.id).checked){
                this.GisData[i].geo_layers=checkedNodes.children.map((item)=>{return item.item_id});
              }else{
                this.GisData[i].geo_layers=[]
              }
            }
          }
        }
        if(checkedState){
          this.getCheckedNodes.push(checkedNodes)
        }else{
           this.getCheckedNodes.splice(this.getCheckedNodes.indexOf(checkedNodes), 1);
        }
        this.SelectedState=checkedState;
        this.$emit('task-tree',this.NewDataList,this.getCheckedNodes);
      },

      unique(arr){
        var hash=[];
        for (var i = 0; i < arr.length; i++) {
          if(arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])){
            hash.push(arr[i]);
          }
        }
        return hash;
      },
			
			//重构
			DataSetInfo(){
				let countData;
				if(this.$route.path=='/taskModule'){
					countData = JSON.parse(localStorage.getItem('batchInfo'));
				}else{
					if(this.infoDatas!=null){
						countData=this.infoDatas.extend_info;
					}
				}
				this.taskTreeData=this.$getWords(104);
				for(let i=0;i<this.taskTreeData.length;i++){
					for (var k in countData.count) {
						if(k==this.taskTreeData[i].item_id){
								this.taskTreeData[i].item_name=this.taskTreeData[i].item_name+'('+countData.count[k]+')'
						}
					}
				}
			},
			
			handleCheckedCitiesChange(){
				this.$emit('task-tree',this.checkedCities);
			},

      //返回Gis数据列表
      async GisList(cont,points,getZoom){
        let params={
          conditions:this.GisData,
          x_min:cont.x_min,
          y_min:cont.y_min,
          x_max:cont.x_max,
          y_max:cont.y_max
        }
        let map = await this.SubDataSetService.realMap(params);
        bus.$emit('TreedataInfo', this.gcoord (map.data),points,getZoom);
      },

      gcoord (content){
        this.commonData=[];
        if(content!=[]){
          if(content[0]!=undefined){
            for(let i=0;i<content.length;i++){
              for(let j=0;j<content[i].length;j++){
                var layer=content[i][j].geo_layer;
                var geojson = content[i][j].geo_json;
                gcoord.transform( geojson, gcoord.WebMercator,gcoord.BD09 );
                let Maplist={
                  ColorProperty:106000,
                  geo_layer:layer,
                  Point:geojson.coordinates,
                  type:content[i][j].geo_json.type,
									logintxt:this.$route.path,
                }
                this.commonData.push(Maplist)
              }
            }
          }
        }
        return this.commonData
      },
    },
    mounted() {
			if(this.$route.path=='/taskModule'){
				setTimeout(()=>{
					this.DataSetInfo();
				},1000)
			}
      let _this=this;
      bus.$on('BoundsPoint', function (content,points,getZoom) {
        _this.GisList(content,points,getZoom)
      })
			bus.$on('infoData', function (res) {
				_this.infoDatas = res;
				_this.DataSetInfo();
			})
			
			//重构
			//获取子集列表对应个数
// 			bus.$on('batchInfo', (res)=>{
// 					_this.batchData = res;
// 			});

  
    },
  };
</script>
<style>
 .tree .el-tree-node__content .el-checkbox{
    display: none!important;
  }
 .tree .el-tree-node__children .el-checkbox{
   display: block!important;
 }
 .Tasktree label{
	 display: block;
	 margin-left: 30px;
	 padding: 10px 0;
	 padding-bottom: 0;
 }
</style>
